#include "LoadScreen.h"
#include "../../SGD_Wrappers/CSGD_Direct3D.h"
#include "../../SGD_Wrappers/CSGD_DirectInput.h"
#include "../../SGD_Wrappers/CSGD_TextureManager.h"
#include "../../SGD_Wrappers/CSGD_XAudio2.h"
#include "../../SGD_Wrappers/SGD_Math.h"
#include "../../SGD_Wrappers/SGD_String.h"
#include "GamePlayState.h"
#include "../Game.h"
#include <fstream>
#include <iostream>
using namespace std;

CLoadScreen::CLoadScreen(void)
{
	m_nCursorPos = 1;
}
CLoadScreen::~CLoadScreen(void)
{
}
CLoadScreen*	CLoadScreen::GetInstance( void )
{
	//Store an instance in global memory
	static CLoadScreen s_Instance;

	//Return the instance
	return &s_Instance;
}
void	CLoadScreen::Enter	( void )
{
	// Store the SGD Wrapper singletons
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();

	ifstream fin;

	fin.open("Save1.txt",ios_base::in);

	if(fin.good())
		{
			fin >> m_nCurrLevel1;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nCurrFloor1;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nEnemies1; 
		}

	fin.close();

	fin.open("Save2.txt",ios_base::in);

	if(fin.good())
		{
			fin >> m_nCurrLevel2;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nCurrFloor2;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nEnemies2; 
		}

	fin.close();

	fin.open("Save3.txt",ios_base::in);

	if(fin.good())
		{
			fin >> m_nCurrLevel3;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nCurrFloor3;
			fin.ignore(INT_MAX, '\n');
			fin >> m_nEnemies3; 
		}

	fin.close();
}
void	CLoadScreen::Exit	( void )
{

}
bool CLoadScreen::Input( void )
{	 

	if(m_pDI->KeyPressed(DIK_RETURN))
	{
		CGamePlayState::GetInstance()->SetLoad(m_nCursorPos);
		CGame::GetInstance()->PopState();
	}

	if(m_pDI->KeyPressed(DIK_DOWNARROW))
		m_nCursorPos++;
	if(m_pDI->KeyPressed(DIK_UPARROW))
		m_nCursorPos--;

	if(m_nCursorPos < 1)
		m_nCursorPos = 3;
	if(m_nCursorPos > 3)
		m_nCursorPos = 1;

	 return true;
}	 
void CLoadScreen::Update( float fElapsedTime )
{	 
	 
}	 
void CLoadScreen::Render( void )
{
	m_pD3D->Clear( 255, 0, 0 );
	TOSTRINGSTREAM ToScreen;
	RECT Cursor = {0,0,0,0};
	if(m_nCursorPos == 1)
	{
		Cursor.left = 50;
		Cursor.top = 100;
		Cursor.bottom = 130;
		Cursor.right = 90;
		m_pD3D->DrawRect(Cursor,0,255,255);
	}

	if(m_nCursorPos == 2)
	{
		Cursor.left = 50;
		Cursor.top = 180;
		Cursor.bottom = 210;
		Cursor.right = 90;
		m_pD3D->DrawRect(Cursor,0,255,255);
	}

	if(m_nCursorPos == 3)
	{
		Cursor.left = 50;
		Cursor.top = 260;
		Cursor.bottom = 290;
		Cursor.right = 90;
		m_pD3D->DrawRect(Cursor,0,255,255);
	}

	m_pD3D->DrawText(_T("Save1: "),100,100,0,255,255);
	ToScreen << "Kills: " << m_nEnemies1 << " Current Level: " << m_nCurrLevel1 << " Current Floor: " << m_nCurrFloor1;
	m_pD3D->DrawText(ToScreen.str().c_str(),230, 100, 0, 255, 255);
	ToScreen.str(_T(""));
	ToScreen << "Kills: " << m_nEnemies2 << " Current Level: " << m_nCurrLevel2 << " Current Floor: " << m_nCurrFloor2;
	m_pD3D->DrawText(_T("Save2: "),100,180,0,255,255);
	m_pD3D->DrawText(ToScreen.str().c_str(),230,180,0,255,255);
	ToScreen.str(_T(""));
	ToScreen << "Kills: " << m_nEnemies3 << " Current Level: " << m_nCurrLevel3 << " Current Floor: " << m_nCurrFloor3;
	m_pD3D->DrawText(_T("Save3: "),100,260,0,255,255);
	m_pD3D->DrawText(ToScreen.str().c_str(),230,260,0,255,255);
}
void CLoadScreen::HandleEvent(CEvent* pEvent)
{

}